import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'
import Layout from '@/components/layout'
import cookie from '@/utils/cookie'

Vue.use(Router)

const routes = [{
  path: '*',
  component: () =>
      import('@/views/extension/404')
},
{
  path: '/',
  component: () =>
      import('@/views/index'),
  async beforeEnter (to, from, next) {
    if (cookie.get('device_id') && !sessionStorage.getItem('token')) {
      sessionStorage.setItem('token', cookie.get('device_id'))
    }
    if (sessionStorage.getItem('token')) {
      await store.dispatch('getUserInfo')
      next('/dashboard')
    }
    next()
  }
},
{
  path: '/index',
  meta: {
    auth: false,
    title: '登录'
  },
  component: () =>
      import('@/views/index')
},
{
  path: '/register',
  meta: {
    auth: false,
    title: '注册'
  },
  component: () =>
      import('@/views/register')
},
{
  path: '/set',
  component: Layout,
  redirect: '/set/index',
  children: [{
    path: '/set/index',
    meta: {
      auth: true,
      title: '设置'
    },
    component: () =>
          import('@/views/set/index')
  },
  {
    path: '/set/nickname',
    meta: {
      auth: true,
      title: '用户名修改'
    },
    component: () =>
          import('@/views/set/nickname')
  },
  {
    path: '/set/sex',
    meta: {
      auth: true,
      title: '性别修改'
    },
    component: () =>
          import('@/views/set/sex')
  },
  {
    path: '/set/pwd',
    meta: {
      auth: true,
      title: '密码修改'
    },
    component: () =>
          import('@/views/set/pwd')
  },
  {
    path: '/set/mobile',
    meta: {
      auth: true,
      title: '更换绑定手机'
    },
    component: () =>
          import('@/views/set/mobile')
  },
  {
    path: '/set/email',
    meta: {
      auth: true,
      title: '绑定邮箱'
    },
    component: () =>
          import('@/views/set/email')
  },
  {
    path: '/set/updateEmail',
    meta: {
      auth: true,
      title: '更换绑定邮箱'
    },
    component: () =>
          import('@/views/set/updateEmail')
  }
  ]
},
{
  path: '/dashboard',
  component: Layout,
  redirect: '/dashboard/index',
  children: [{
    path: '/dashboard/index',
    component: () =>
        import('@/views/dashboard'),
    name: 'dashboard',
    meta: {
      auth: true,
      title: '我的主页'
    }
  }]
},
{
  path: '/notice',
  component: Layout,
  redirect: '/notice/index',
  children: [{
    path: '/notice/index',
    component: () =>
        import('@/views/notice'),
    name: 'notice',
    meta: {
      auth: true,
      title: '消息中心'
    }
  }]
},
{
  path: '/noticeDetails',
  component: Layout,
  redirect: '/noticeDetails/index',
  children: [{
    path: '/noticeDetails/index',
    component: () =>
        import('@/views/notice/details'),
    name: 'noticeDetails',
    meta: {
      auth: true,
      title: '消息详情'
    }
  }]
},
{
  path: '/noticeSend',
  component: Layout,
  redirect: '/noticeSend/index',
  children: [{
    path: '/noticeSend/index',
    component: () =>
        import('@/views/notice/send'),
    name: 'noticeSend',
    meta: {
      auth: true,
      title: '消息详情'
    }
  }]
},
{
  path: '/myStars',
  component: Layout,
  redirect: '/myStars/index',
  children: [{
    path: '/myStars/index',
    component: () =>
        import('@/views/my-stars'),
    name: 'myStars',
    meta: {
      auth: true,
      title: '我的关注'
    }
  }]
},
{
  path: '/cooperation',
  component: Layout,
  redirect: '/cooperation/index',
  children: [{
    path: '/cooperation/index',
    component: () =>
        import('@/views/cooperation'),
    name: 'cooperation',
    meta: {
      auth: false,
      title: '商务合作'
    }
  }]
},
{
  path: '/about',
  component: Layout,
  redirect: '/about/index',
  children: [{
    path: '/about/index',
    component: () =>
        import('@/views/about'),
    name: 'about',
    meta: {
      auth: false,
      title: '关于我们'
    }
  }]
},
{
  path: '/protocol',
  component: Layout,
  redirect: '/protocol/index',
  children: [{
    path: '/protocol/index',
    component: () =>
        import('@/views/protocol'),
    name: 'protocol',
    meta: {
      auth: false,
      title: '注册协议'
    }
  }]
},
{
  path: '/feedback',
  component: Layout,
  redirect: '/feedback/index',
  children: [{
    path: '/feedback/index',
    component: () =>
        import('@/views/feedback'),
    name: 'feedback',
    meta: {
      auth: false,
      title: '反馈中心'
    }
  }]
},
{
  path: '/article/:id',
  meta: {
    auth: false,
    title: '文章内容详情'
  },
  component: () => import('@/views/article/detail')
}
]

const router = new Router({
  // mode: 'history',
  routes,
  scrollBehavior (to, from, savedPosition) {
    return savedPosition || {
      x: 0,
      y: 0
    }
  }
})

router.beforeEach(async (to, from, next) => {
  // 不需要登录
  if (!to.matched.some(record => record.meta.auth)) {
    next()
    return
  }
  next()
  let token = sessionStorage.getItem('token')
  if (token) {
    // 登录后强制刷新页面，用户信息从sessionStorage获取
    let me = sessionStorage.getItem('me')
    // let jsonMe = JSON.parse(me)
    // console.log(jsonMe)
    if (me) {
      store.commit('GET_USER_INFO', JSON.parse(me))
      next()
    } else {
      let res = await store.dispatch('getUserInfo')
      if (res.user.userAccount) {
        next()
      }
    }
  } else {
    next('/index')
  }

  // 未登录，本地也无token，跳转到登录页，同时记录来源页，登录后可跳转到相应页面
  // next(`/?redirectPath=${to.fullPath}`)
})

export default router
